package com.aisile.user.service.impl;

import com.aisile.common.exception.CustomExceptionCatch;
import com.aisile.model.article.pojos.ApAuthor;
import com.aisile.model.common.dtos.ResponseResult;
import com.aisile.model.common.enums.AppHttpCodeEnum;
import com.aisile.model.media.pojos.WmUser;
import com.aisile.model.user.dtos.AuthDto;
import com.aisile.model.user.pojos.ApUser;
import com.aisile.model.user.pojos.ApUserIdentity;
import com.aisile.model.user.pojos.ApUserRealname;
import com.aisile.user.client.ArticleFeign;
import com.aisile.user.client.WemediaFeign;
import com.aisile.user.mapper.ApUserIdentityMapper;
import com.aisile.user.mapper.ApUserMapper;
import com.aisile.user.mapper.ApUserRealnameMapper;
import com.aisile.user.service.ApUserIdentityService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Auther:yry
 * @Date:2024/1/2 0002
 * @VERSON:1.0
 */
@Service
@SuppressWarnings("ALL")
public class ApUserIdentityServiceImpl extends ServiceImpl<ApUserIdentityMapper, ApUserIdentity> implements ApUserIdentityService {

    @Autowired
    private WemediaFeign wemediaFeign;

    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;



    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {
        return null;
    }

    /**
     * 人工审核方法
     *
     * @param dto
     * @param type
     * @return
     */
    @Override
    @GlobalTransactional
    public ResponseResult updateStatusById(AuthDto dto, Short type) {

        if (dto == null || dto.getId() == null) {
            CustomExceptionCatch.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if(type == null || (!type.equals((short)2) && !type.equals((short)9))){
            CustomExceptionCatch.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // status = 1
        ApUserIdentity apUserIdentity =  this.getById(dto.getId());
        if(apUserIdentity == null || apUserIdentity.getStatus() !=(short)1){
            CustomExceptionCatch.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 1. 只能实名认证 才能身份认证
        ApUserRealname apUserRealname =  apUserRealnameMapper.selectOne(Wrappers.<ApUserRealname>lambdaQuery().eq(ApUserRealname::getUserId,apUserIdentity.getUserId()));
        if(apUserRealname == null || apUserRealname.getStatus()!= (short)9){
            CustomExceptionCatch.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 2. 通过
        // 3. 不通过
        // status = 9
        // status = 2
        apUserIdentity.setStatus(type);
        if(!"".equals(dto.getMsg())){
            apUserIdentity.setReason(dto.getMsg());
        }
        // 修改表
        this.updateById(apUserIdentity);
        // feign
        if(type.equals((short)9)){
            // 调用feign
            createWemediaAndAuthor(apUserIdentity);
        }
//        int i = 9/0;
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }



    private void createWemediaAndAuthor(ApUserIdentity apUserIdentity){
        // 查询用户信息
        Integer userId = apUserIdentity.getUserId();
        ApUser apUser =  apUserMapper.selectById(userId);
        if(apUser == null){
            CustomExceptionCatch.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 创建自媒体账号
        WmUser wmUser =  wemediaFeign.findByName(apUser.getName());
        if(wmUser == null){
            // 添加
            wmUser = new WmUser();
            wmUser.setApUserId(apUser.getId()); // 用户id
            wmUser.setApAuthorId(0);
            wmUser.setName(apUser.getName());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setNickname(apUser.getName());
            wmUser.setImage(apUser.getImage());
            wmUser.setLocation("默认");
            wmUser.setPhone(apUser.getPhone());
            wmUser.setStatus(9);
            wmUser.setType(0);
            wmUser.setScore(100);
            wmUser.setCreatedTime(new Date());
            wmUser = wemediaFeign.save(wmUser);
        }
        // 创建作者
        ApAuthor apAuthor =  createAuthor(wmUser);
        // 反向维护 apuser表
        apUser.setIdentityAuthentication(true);
        apUser.setFlag((short)1);
        apUserMapper.updateById(apUser);
    }

    private ApAuthor createAuthor(WmUser wmUser){
        ApAuthor apAuthor = null;
        if(wmUser!=null && wmUser.getApUserId()!=null){
            apAuthor =  articleFeign.findByUserId(wmUser.getApUserId());
            if(apAuthor == null){
                apAuthor = new ApAuthor();
                apAuthor.setWmUserId(wmUser.getId());
                apAuthor.setCreatedTime(new Date());
                apAuthor.setUserId(wmUser.getApUserId());
                apAuthor.setType(2);
                apAuthor.setName(wmUser.getName());
                apAuthor =  articleFeign.save(apAuthor);
            }
        }
        return apAuthor;
    }

}
